<template>
  <div>
    <div class="box">
      <div class="nav">
        <i class="el-icon-s-home"></i>
        <router-link to="/" style="color: inherit; text-decoration: none;">
          <span>首页</span>
        </router-link>
        / <span>正文</span>
      </div>
      <h2>{{ List.title || '加载中...' }}</h2>
      <div class="text">
        <i class="el-icon-s-order"></i> <span>发表于{{ List.updateTime || '未知时间' }}</span>
        <i class="el-icon-folder"></i> <span>分类于{{ List.categoryName || '未分类' }}</span>
        <i class="el-icon-view"></i> <span>阅读量{{ List.readNum || 0 }}</span>
      </div>
      <p>{{ List.content || '内容加载中...' }}</p>
      <!-- 标签容错渲染 -->
      <span v-if="List.tags && List.tags.length > 0" style="padding: 5px; background-color: #8aff9b; border-radius: 10px;">
        #{{ List.tags[0].name }}
      </span>
      <span v-else style="padding: 5px; background-color: #f5f5f5; border-radius: 10px; color: #999;">
        #暂无标签
      </span>
      <!-- 上下篇容错渲染 -->
      <div class="cdcd">
        <div>
          <p>&lt; 上一篇</p>
          <span>{{ List.preArticle?.title || '无' }}</span>
        </div>
        <div>
          <p>下一篇 &gt;</p>
          <span>{{ List.nextArticle?.title || '无' }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { indexMore } from "../../../units/api";
export default {
  data() {
    return {
      List: {
        title: '',
        updateTime: '',
        categoryName: '',
        readNum: 0,
        content: '',
        tags: [{ name: '' }],
        preArticle: {},
        nextArticle: {}
      }
    };
  },
  created() {
    this.getListAPI();
  },
  methods: {
    async getListAPI() {
      try {
        const res = await indexMore({ articleId: this.$route.params.id });
        const data = res.data.data || {};
        // 确保tags始终是数组
        const tags = Array.isArray(data.tags) ? data.tags : [];
        this.List = {
          ...this.List,
          ...data,
          tags,
          preArticle: data.preArticle || {},
          nextArticle: data.nextArticle || {}
        };
      } catch (error) {
        console.error('文章数据请求失败：', error);
        this.List.content = '文章加载失败，请刷新页面重试';
      }
    }
  },
  // 监听路由变化，刷新数据
  watch: {
    '$route.params.id': function() {
      this.getListAPI();
    }
  }
};
</script>

<style scoped lang="scss">
.box {
  background-color: #fff;
  width: 100%;
  min-height: 300px;
  line-height: 20px;
  text-align: left;
  box-sizing: border-box;
  padding: 20px;

  &>h2 {
    margin: 10px 0 16px;
    color: #333;
  }

  &>p {
    color: #738dff;
    text-decoration: underline;
    font-style: italic;
    font-weight: bold;
    line-height: 1.8;
    margin: 16px 0;
  }

  .nav {
    font-size: 12px;
    color: #666;
    span {
      margin: 0 4px;
    }
    span:hover {
      color: #738dff;
      cursor: pointer;
    }
  }

  .text {
    font-size: 12px;
    color: gray;
    span {
      margin: 0 8px 0 5px;
    }
  }

  .cdcd {
    margin-top: 25px;
    border-top: 1px solid #666;
    display: flex;
    justify-content: space-between;
    align-items: center;
    div {
      cursor: pointer;
    }
    p {
      font-size: 12px;
      color: #666;
    }
    span:hover {
      color: #738dff;
    }
  }
}

</style>